import '../http/request_api.dart';
import '../model/base_response.dart';
import 'base_api_service.dart';
import 'user_storage_service.dart';

class ContractService extends BaseApiService {
  static final ContractService _instance = ContractService._internal();

  factory ContractService() {
    return _instance;
  }

  ContractService._internal();

  /// 创建广告合约
  Future<BaseResponse<Map<String, dynamic>>> createAdsContract({
    required int rewardAmount,
    required String rewardType,
    required String adUnitId,
    Map<String, dynamic>? additionalParams,
  }) async {
    final data = {
      // "xtest": "1024CxNqmt0LTa4IckJ",
    };

    return await post<Map<String, dynamic>>(
      RequestApi.CREATE_ADS_CONTRACT,
      showLoading: true,
      fromJsonT: (data) => data as Map<String, dynamic>,
    );
  }

  /// 创建签到合约
  Future<BaseResponse<Map<String, dynamic>>> createDailySignContract({
    required String signDate,
    Map<String, dynamic>? additionalParams,
  }) async {
    final data = {
      "contract_type": "daily_sign",
      "sign_date": signDate,
      "timestamp": DateTime.now().millisecondsSinceEpoch,
      ...?additionalParams,
    };

    return await post<Map<String, dynamic>>(
      RequestApi.CREATE_DAILY_CONTRACT, // 需要在RequestApi中添加
      data: data,
      showLoading: true,
      fromJsonT: (data) => data as Map<String, dynamic>,
    );
  }

  /// 创建订阅合约
  Future<BaseResponse<Map<String, dynamic>>> createSubscriptionContract({
    required String subscriptionId,
    required String subscriptionType,
    required double amount,
    Map<String, dynamic>? additionalParams,
  }) async {
    final data = {
      "contract_type": "subscription",
      "subscription_id": subscriptionId,
      "subscription_type": subscriptionType,
      "amount": amount,
      "timestamp": DateTime.now().millisecondsSinceEpoch,
      ...?additionalParams,
    };

    return await post<Map<String, dynamic>>(
      RequestApi.CREATE_SUBSCRIPTION_CONTRACT, // 需要在RequestApi中添加
      data: data,
      showLoading: true,
      fromJsonT: (data) => data as Map<String, dynamic>,
    );
  }

  /// 购买付费合约
  Future<BaseResponse<Map<String, dynamic>>> purchasePaidContract({
    required String contractId,
    required String paymentMethod,
    required double amount,
    Map<String, dynamic>? additionalParams,
  }) async {
    final data = {
      "contract_id": contractId,
      "payment_method": paymentMethod,
      "amount": amount,
      "timestamp": DateTime.now().millisecondsSinceEpoch,
      ...?additionalParams,
    };

    return await post<Map<String, dynamic>>(
      RequestApi.PURCHASE_PAID_CONTRACT, // 需要在RequestApi中添加
      data: data,
      showLoading: true,
      fromJsonT: (data) => data as Map<String, dynamic>,
    );
  }
}